import { sortBy } from 'lodash-es';
const dices = ['yellow', 'green', 'blue', 'orange', 'purple', 'white'];
export const bgColor = {
  yellow: '#F9DA4A',
  green: '#02A425',
  blue: '#1276BE',
  orange: '#EE7C01',
  purple: '#571788',
  white: '#fff',
} as any;
export const drapDice = () => {
  return Math.ceil(Math.random() * 6);
};

export const drapDices = (dices: any[]) => {
  return dices.map(i => ({ ...i, value: drapDice() }));
};
type dices = Readonly<{
  rockPool: readonly {
    type: string;
    value: number;
  }[];
  chosen: any[];
  locked: any[];
}>;

export const findDiceByType = (dices: dices, type: string) => {
  const d = dices.rockPool.find(d => d?.type === type);
  const d2 = dices.chosen.find(d => d?.type === type);
  const d3 = dices.locked.find(d => d?.type === type);
  return d || d2 || d3;
};

type Dices = { value: number; type: string }[];

export const dividLittle3 = (dices: Dices) => {
  const small = [];
  const sortedArr = sortBy(dices, 'value');

  var l1 = sortedArr.shift();
  small.push(l1);
  var newArr: Dices = [];
  sortedArr.forEach(i => {
    console.log('i', i.type);
    if (i?.value === l1?.value) {
      small.push(i);
    } else {
      newArr.push(i);
    }
  });
  if (small.length >= 3) {
    return {
      small,
      large: newArr,
    };
  }

  var l2 = newArr.shift();
  small.push(l2);
  var newArr2: Dices = [];
  newArr.forEach(i => {
    if (i?.value === l2?.value) {
      small.push(i);
    } else {
      newArr2.push(i);
    }
  });
  if (small.length >= 3) {
    return {
      small,
      large: newArr2,
    };
  }

  var l3 = newArr2.shift();
  small.push(l3);
  var newArr3: Dices = [];
  newArr2.forEach(i => {
    if (i?.value === l3?.value) {
      small.push(i);
    } else {
      newArr3.push(i);
    }
  });
  return {
    small,
    large: newArr3,
  };
};
